//
//  VCDebugView.m
//  VCDemo
//
//  Created by 肖康龙 on 2017/9/14.
//  Copyright © 2017年 vsoontech. All rights reserved.
//

#import "VCDebugView.h"

@interface VCDebugView ()

/**三个ts的信息*/
@property (nonatomic,strong)NSMutableArray *array_Ts;
/**播放信息*/
@property (nonatomic,strong)UILabel *label_info;
/**播放信息*/
@property (nonatomic,strong)NSMutableArray *array_ipInfo;
/**包的下载信息 512个*/
@property (nonatomic,strong)NSMutableArray *array_pack;

/**灰色*/
@property (nonatomic,strong)UIColor *defaultColor;
/**红色第一个*/
@property (nonatomic,strong)UIColor *redColor;
/**绿色第二个*/
@property (nonatomic,strong)UIColor *greenColor;
/**蓝色第三个*/
@property (nonatomic,strong)UIColor *blueColor;
/**黄色重复颜色*/
@property (nonatomic,strong)UIColor *yellowColor;
@end

@implementation VCDebugView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatView];
        self.userInteractionEnabled = NO;
    }
    return self;
}

/**创建view*/
-(void)creatView{
    self.array_ipInfo = [NSMutableArray array];
    self.array_pack = [NSMutableArray array];
    self.array_Ts = [NSMutableArray array];
    
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.35];
    
    self.redColor = [UIColor redColor];
    self.greenColor = [UIColor greenColor];
    self.blueColor = [UIColor blueColor];
    self.yellowColor = [UIColor yellowColor];
    self.defaultColor = [UIColor colorWithWhite:0.7 alpha:1];
    
    for (int i = 0; i < 3; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 130*i, [UIScreen mainScreen].bounds.size.width, 54)];
        label.numberOfLines = 0;
        label.tag = i+10;
        label.font = [UIFont systemFontOfSize:11];
        label.textColor = [UIColor whiteColor];
        [self addSubview:label];
        label.text = @"text";
        [self.array_Ts addObject:label];
        label.hidden = YES;
    }
    for (int i = 0; i < 3; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 54 + 130*i, [UIScreen mainScreen].bounds.size.width, 66)];
        label.numberOfLines = 0;
        label.tag = i+10;
        label.font = [UIFont systemFontOfSize:11];
        label.textColor = [UIColor whiteColor];
        [self addSubview:label];
        label.text = @"text";
        [self.array_ipInfo addObject:label];
        label.hidden = YES;
    }
    
    self.label_info = [[UILabel alloc] initWithFrame:CGRectMake(0, 130*3, [UIScreen mainScreen].bounds.size.width, 50)];
    self.label_info.numberOfLines = 0;
    self.label_info.textColor = [UIColor whiteColor];
    self.label_info.font = [UIFont systemFontOfSize:10];
    [self addSubview:self.label_info];
    self.label_info.text = @"textInfo";
    
    
    CGFloat viewHeight = [UIScreen mainScreen].bounds.size.height - 440;
    NSInteger colNum = [UIScreen mainScreen].bounds.size.width /12;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 440, [UIScreen mainScreen].bounds.size.width, viewHeight)];
    view.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    [self addSubview:view];
    
    for (int i = 0; i < 1024; i++) {
        CALayer *layer = [[CALayer alloc] init];
        layer.backgroundColor = self.defaultColor.CGColor;
        
        layer.frame = CGRectMake(i % colNum * 12, i/colNum * 7, 10, 5);
        
        [view.layer addSublayer:layer];
        [self.array_pack addObject:layer];
    }
    
}


-(void)setDucation:(NSTimeInterval)ducation{
    _ducation = ducation;
    self.label_info.text = [NSString stringWithFormat:@"已下载时长/已播放时长 /%lf",ducation];
}

/**
 *DownloadedTime
 *PlayTime
 *noCatonTime
 *catonTimer
 *catonNum
 */
-(void)alreadyDownloadedTime:(NSTimeInterval)DownloadedTime
             alreadyPlayTime:(NSTimeInterval)PlayTime
                 noCatonTime:(NSTimeInterval)noCatonTime
                  catonTimer:(NSTimeInterval)catonTimer
                    catonNum:(int)catonNum{
    self.label_info.text = [NSString stringWithFormat:@"已下载时长/已播放时长 %lf/%lf\n持续播放时间 %lf\n总缓冲时间 %lf\n总缓冲次数 %i",DownloadedTime,PlayTime,noCatonTime,catonTimer,catonNum];
}

/**重置
 1红色 2绿色 3蓝色
 */
-(void)resetViewWithIndex:(NSUInteger)index{
    switch (index) {
        case 1:
            for (CALayer *layer in self.array_pack) {
                layer.backgroundColor = self.redColor.CGColor;
            }
            break;
        case 2:
            for (CALayer *layer in self.array_pack) {
                layer.backgroundColor = self.greenColor.CGColor;
            }
            break;
        case 3:
            for (CALayer *layer in self.array_pack) {
                layer.backgroundColor = self.blueColor.CGColor;
            }
            break;
        default:
            for (CALayer *layer in self.array_pack) {
                layer.backgroundColor = self.defaultColor.CGColor;
            }
            break;
    }
    
}

/**设置包颜色
 *index 颜色 1红色 2绿色 3蓝色
 *startPoint 开始的位置
 *bitmap 已经下载的位置
 */
-(void)setPageColorWithIndex:(NSUInteger)index startPoint:(NSUInteger)startPoint bitmap:(NSData *)bitmap{
    
    Byte *byte = (Byte *)[bitmap bytes];
    
    for (int i = 0;i < bitmap.length; i++) {
        for (int j = 0; j < 8; j++) {
            char num = byte[i]>>j & 0x01;
            if (num == 1) {//改变颜色
                
                [self setPageColorWithIndex:index indePage:i*8+j + startPoint];
            }
        }
        
    }
}

/**设置包颜色
 *index 颜色
 *indexPage 第几个完成
 */
-(void)setYellowPageColorWithIndex:(NSUInteger)index indePage:(NSUInteger)indexPage{
    if (self.array_pack.count > indexPage) {
        CALayer *layer = self.array_pack[indexPage];
        if (layer.backgroundColor != self.defaultColor.CGColor) {
            layer.backgroundColor = self.yellowColor.CGColor;
        }
    }
}

/**设置包颜色
 *index 颜色
 *indexPage 第几个完成
 */
-(void)setPageColorWithIndex:(NSUInteger)index indePage:(NSUInteger)indexPage{
    
    if (self.array_pack.count > indexPage) {
        CALayer *layer = self.array_pack[indexPage];
        if (layer.backgroundColor == self.defaultColor.CGColor) {
            switch (index) {
                case 1:
                    layer.backgroundColor = self.redColor.CGColor;
                    break;
                case 2:
                    layer.backgroundColor = self.greenColor.CGColor;
                    break;
                case 3:
                    layer.backgroundColor = self.blueColor.CGColor;
                    break;

            }
        }
    }
}

/**
 *tsName:片段名字
 *fileSize:片段大小
 *tsIndex :第几个下载器
 *timer:已耗时
 *ip:host地址
 *bitMap :当前下载量
 *instantaneousSpeed:瞬时速度
 *downloadTotal:下载总量
 *tunnelId
 *state 当前处于的状态 0打通道Tunnel 1订阅subcribe 2传输数据data状态
 */
-(void)setTsName:(NSString *)tsName
        fileSize:(NSUInteger)fileSize
          bitMap:(NSData *)bitMap
           timer:(NSTimeInterval)timer
         tsIndex:(NSUInteger)tsIndex{
    if (tsIndex < self.array_Ts.count) {
        UILabel *label = self.array_Ts[tsIndex];
        
        CGFloat downloadTS = 0;
        Byte *byte = (Byte *)[bitMap bytes];
        
        for (int i = 0;i < bitMap.length; i++) {
            for (int j = 0; j < 8; j++) {
                char num = byte[i]>>j & 0x01;
                downloadTS += num * 1400;
            }
            
        }
        
        int percentage = (int)(downloadTS/fileSize*100);
        
        label.text = [NSString stringWithFormat:@"片段名称 %@\n文件大小 %lf MB\n当前已下载 %lf MB(%i%%)\n已耗时 %lf",tsName,fileSize/1024.0/1024.0,downloadTS/1024.0/1024.0,percentage,timer];
    }
}
/**
 *tsIndex :第几个下载器
 *ip:host地址
 *bitMap :当前下载量
 *instantaneousSpeed:瞬时速度
 *downloadTotal:下载总量
 *tunnelId
 *state 当前处于的状态 0打通道Tunnel 1订阅subcribe 2传输数据data状态
 */
-(void)settsIndex:(NSUInteger)tsIndex
             Tsip:(NSString *)ip
instantaneousSpeed:(NSUInteger)instantaneousSpeed
    downloadTotal:(NSUInteger)downloadTotal
         tunnelId:(NSInteger)tunnelId
            state:(int)state;{
    if (tsIndex < self.array_ipInfo.count) {
        UILabel *label = self.array_ipInfo[tsIndex];
        NSString *stateString;
        switch (state) {
            case 0:
                stateString = @"Tunnel";
                break;
            case 1:
                stateString = @"Subcribe";
                break;
            case 2:
                stateString = @"DATA";
                break;
            default:
                break;
        }
        
        label.text = [NSString stringWithFormat:@"IP %@\n瞬时速度 %lfKB/S\n总下载量 %lfMB\n当前状态 %@\ntunnelId %ld",ip,instantaneousSpeed/1024.0,downloadTotal/1024.0/1024.0,stateString,tunnelId];
    }
}
/**完成隐藏*/
-(void)completeHiddenWithIndex:(NSUInteger)index{
    if (index < self.array_Ts.count) {
        UILabel *label = self.array_Ts[index];
        label.hidden = YES;
        UILabel *ipInfo = self.array_ipInfo[index];
        ipInfo.hidden = YES;
    }
}
/**开始显示*/
-(void)startShowWithIndex:(NSUInteger)index{
    if (index < self.array_Ts.count) {
        UILabel *label = self.array_Ts[index];
        label.hidden = NO;
        UILabel *ipInfo = self.array_ipInfo[index];
        ipInfo.hidden = NO;
    }
}
@end
